package com.proton.carepatchtemp.login.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.activity.MainActivity;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.database.dao.RegisterInfo;
import com.proton.carepatchtemp.database.dao.SignActvts;
import com.proton.carepatchtemp.utils.FormUtils;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.VaildUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.SignCenter;
import com.proton.carepatchtemp.utils.ui.ToastUtils;
import com.sdsmdg.tastytoast.TastyToast;

import cn.pedant.SweetAlert.SweetMessageDialog;
import cn.pedant.SweetAlert.Type;

/**
 *
 */
public class LoginAuthActivity extends BaseActivity implements View.OnClickListener {

    RegisterInfo info = new RegisterInfo();
    SweetMessageDialog mDialog;
    //登录页面控件
    private ImageButton mBtn_back;
    private EditText mEt_account;
    private EditText mEt_pwd;
    private TextView mTv_forget_pwd;
    private Button mBtn_login;
    private TextView mTv_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_auth);
        bindViews();
        setListener();
        mDialog = new SweetMessageDialog(this);
    }


    // End Of Content View Elements

    private void bindViews() {
        mBtn_back = findViewById(R.id.login_back_btn);
        mEt_account = findViewById(R.id.et_name);
        mEt_pwd = findViewById(R.id.et_pwd);
        mTv_forget_pwd = findViewById(R.id.tv_forget_pwd);
        mBtn_login = findViewById(R.id.btn_login);
        mTv_register = findViewById(R.id.tv_register);
    }

    //设置点击监听
    @Override
    public void setListener() {
        //  返回按钮点击监听
        mBtn_back.setOnClickListener(this);

        //  登录按钮点击监听
        mBtn_login.setOnClickListener(this);

        //  忘记密码点击监听
        mTv_forget_pwd.setOnClickListener(this);

        //  注册TextView点击监听器
        mTv_register.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            //  返回按钮
            case R.id.login_back_btn:
                finish();
                break;

            //  登录按钮
            case R.id.btn_login:
                //登录前客户端对输入内容进行校验
//                doLogin();
                if (loginContentCheck()) {
                    mEt_account.setEnabled(false);
                    mEt_pwd.setEnabled(false);
                    mDialog.setTitleText("登录中...")
                            .changeAlertType(Type.PROGRESS_TYPE)
                            .show();

                    SignCenter.login(info, SignActvts.LOGIN);
                }
                break;

            //  忘记密码
            case R.id.tv_forget_pwd:
                Intent findPwdIntent = new Intent(LoginAuthActivity.this, FindPwdActivity.class);
                startActivity(findPwdIntent);
                break;

            //  去往注册页面
            case R.id.tv_register:
                Intent goRgisterIntent = new Intent(LoginAuthActivity.this, RegisterActivity.class);
                startActivity(goRgisterIntent);
                finish();
                break;
            default:
                break;
        }
    }


    /**
     * 登录动作
     */
    private void doLogin() {
        //Toast.makeText(this,"开始登录",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(LoginAuthActivity.this, MainActivity.class);
        startActivity(intent);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        finish();
    }


    /**
     * 登录前参数合法性检查
     *
     * @return false--->参数不合法   true--->参数合法
     */
    private boolean loginContentCheck() {
        String phone = FormUtils.getString(mEt_account);
        String pwd = FormUtils.getString(mEt_pwd);
        if (TextUtils.isEmpty(phone)) {
            TastyUtils.makeTextShort(R.string.no_value_phone_number, TastyToast.ERROR);
            mEt_account.requestFocus();
            return false;
        }
        if (TextUtils.isEmpty(pwd)) {
            ToastUtils.w(R.string.no_value_pwd);
            mEt_pwd.requestFocus();
            return false;
        }
        if (VaildUtils.valiMobile(phone)) {
            info.setMobile(phone);
        } else {
            ToastUtils.w(R.string.invalid_phone_number);
            mEt_account.requestFocus();
            return false;
        }
        if (VaildUtils.valiPwd(pwd)) {
            info.setPwd(pwd);
        } else {
            ToastUtils.w(R.string.invalid_pwd);
            mEt_pwd.requestFocus();
            return false;
        }

        return true;
    }


    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    @Override
    public void dealEvent(MainEvent event) {
        if (null == event || isFinishing()) {
            return;
        }
        int id = event.getId();
        switch (id) {
            case Codes.NET_SIGN_LOGIN:
                if (event.isSuccess()) {
                    mDialog.setTitleText("登录成功！")
                            .changeAlertType(Type.SUCCESS_MSG_TYPE)
                            .show();
                    mDialog.setOnDismissListener(dialog -> {
                        doLogin();
                    });
                } else {
                    mDialog.setTitleText(event.getMsg())
                            .changeAlertType(Type.ERROR_MSG_TYPE)
                            .show();
//                    Toast.makeText(this, event.getMsg(), Toast.LENGTH_SHORT).show();
                }
                mEt_account.setEnabled(true);
                mEt_pwd.setEnabled(true);
                break;
            default:
                break;
        }
    }

}
